package com.tyk.bookstore.back.member.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.common.core.util.IdGenerator;
import com.tyk.bookstore.back.member.model.query.MemberPageQuery;
import com.tyk.bookstore.back.member.model.query.StateUpdateQuery;
import com.tyk.bookstore.back.member.model.vo.MemberPageVo;
import com.tyk.bookstore.back.member.model.vo.MemberVo;
import com.tyk.bookstore.back.member.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author tyk
 * @description 会员管理
 */
@Tag(name = "会员管理", description = "会员管理")
@RequestMapping({"api/member"})
@RestController
@RequiredArgsConstructor
public class MemberController {
    private final MemberService memberService;

    @GetMapping({"{id}"})
    @Operation(summary = "查询会员详情", description = "查询会员详情")
    @SaCheckPermission("member:get")
    public R<MemberVo> getById(
            @Schema(description = "会员ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(memberService.getById(id));
    }


    @Operation(summary = "分页查询会员", description = "分页查询会员")
    @SaCheckPermission("member:page")
    @GetMapping({"page"})
    public R<MemberPageVo> getPage(
            @Validated @ParameterObject MemberPageQuery query,
            BindingResult br
    ) {
        System.out.println(IdGenerator.id());
        return br.hasErrors() ? R.ok(PageVo.instance(MemberPageVo.class)) : R.ok(memberService.getPage(query));
    }


    @Operation(summary = "修改会员状态", description = "修改会员状态")
    @SaCheckPermission("member:state:update")
    @PutMapping({"state"})
    public R<Object> changeState(
            @Validated @RequestBody StateUpdateQuery query
    ) {
        memberService.changeState(query);
        return R.ok();
    }
}
